package com.huilan.etl.handler;

import com.google.auto.service.AutoService;
import com.huilan.etl.entity.AirlineInfoConfig;
import com.huilan.etl.message.IMessageService;
import com.huilan.etl.service.AirlineInfoConfigService;
import com.huilan.etl.util.ConstantsUtil;
import com.huilan.etl.util.SpringUtil;
import com.huilan.etl.util.XmlUtil;

import java.util.Map;

/**
 * 航空公司数据解析
 *
 * @author ghy
 * @date 2018-12-31 18:40
 **/
@AutoService(IMessageService.class)
public class AirlineHandler implements IMessageService {

    /**
     * 静态类属性集合
     */
    private static Map<String,String> map = ConstantsUtil.getFieldMap(AIRLINE.class);

    /**
     * 航空公司——服务编码静态集合
     */
    private static class AIRLINE{

        /**
         * 航空公司增加事件
         */
        private final static String BEAN_NAME = "Airline";

        /**
         * 航空公司增加事件
         */
        private final static String ADXP_NAOMS_O_BASE_ALIE = "航空公司增加事件";

        /**
         * 航空公司变更事件
         */
        private final static String ADXP_NAOMS_O_BASE_ALUE = "航空公司变更事件";

        /**
         * 航空公司删除事件
         */
        private final static String ADXP_NAOMS_O_BASE_ALDE = "航空公司删除事件";

        /**
         * 航空公司请求消息
         */
        private final static String ADXP_NAOMS_O_BASE_RQAL = "航空公司请求消息";

        /**
         * 航空公司整表同步事件
         */
        private final static String ADXP_NAOMS_O_BASE_ALDL = "航空公司整表同步事件";
    }

    /**
     * 判断是否包含服务编码
     * @param serviceCode 服务编码
     * @return Boolean
     */
    @Override
    public boolean containsKey(String serviceCode) {
        return map.containsKey(serviceCode);
    }

    /**
     * 解析消息内容
     * @param serviceCode 服务编码
     * @param xml 消息内容
     */
    @Override
    public String excute(String serviceCode,String xml) {
        switch (map.get(serviceCode)){
            case AIRLINE.ADXP_NAOMS_O_BASE_ALIE:
                insert(xml);
                break;
            case AIRLINE.ADXP_NAOMS_O_BASE_ALUE:
                update(xml);
                break;
            case AIRLINE.ADXP_NAOMS_O_BASE_ALDE:
                delete(xml);
                break;
            case AIRLINE.ADXP_NAOMS_O_BASE_ALDL:
                saveBatch(xml);
                break;
            default:
                System.out.println("=======");
                break;
        }
        return null;
    }

    private void saveBatch(String xml) {
        System.out.println("批量保存");
        AirlineInfoConfigService airlineInfoConfigService = SpringUtil.getBean(AirlineInfoConfigService.class);
    }

    private void delete(String xml) {
        System.out.println("删除方法");
        AirlineInfoConfigService airlineInfoConfigService = SpringUtil.getBean(AirlineInfoConfigService.class);
        AirlineInfoConfig airlineInfoConfig = XmlUtil.getBodyBean(xml, AirlineInfoConfig.class, AIRLINE.BEAN_NAME);
//        airlineInfoConfigService.removeByMap(airlineInfoConfig);
    }

    private void update(String xml) {
        System.out.println("修改方法");
        AirlineInfoConfigService airlineInfoConfigService = SpringUtil.getBean(AirlineInfoConfigService.class);
        AirlineInfoConfig airlineInfoConfig = XmlUtil.getBodyBean(xml, AirlineInfoConfig.class, AIRLINE.BEAN_NAME);
        airlineInfoConfigService.updateById(airlineInfoConfig);
    }

    private void insert(String xml) {
        System.out.println("添加方法");
        AirlineInfoConfigService airlineInfoConfigService = SpringUtil.getBean(AirlineInfoConfigService.class);
        AirlineInfoConfig airlineInfoConfig = XmlUtil.getBodyBean(xml, AirlineInfoConfig.class, AIRLINE.BEAN_NAME);
        airlineInfoConfigService.save(airlineInfoConfig);
    }


}
